#!/usr/bin/env bash
#
# author:yang
# description: 自动安装 openresty 脚本

DOWNLOAD_URL="https://openresty.org/download/openresty-1.15.8.1.tar.gz"
TAR_PACKAGE_NAME=$(echo ${DOWNLOAD_URL}|awk -F '/' '{print $NF}')
DOWNLOAD_DIR="/data/src"
INSTALL_ROOT_DIR="/data/soft"
INSTALL_DIR=$(echo ${TAR_PACKAGE_NAME}|awk -F '.tar' '{print $1}')
ABS_INSTALL_DIR=${INSTALL_ROOT_DIR}/${INSTALL_DIR}
LINK="/data/openresty"
CPUS=$(cat /proc/cpuinfo|grep "processor"|wc -l)
RUN_USER=nginx
LOG_FILE=/tmp/install_openresty.log

# 日志函数
function _logging() {
	
	local color_start=$1
	local color_end=$2
	local level=$3
	local message=$4
	echo -e ${color_start}"[" `date +'%F %H:%M:%S'` "]" "[" $level "]" $message${color_end}|tee -a ${LOG_FILE}
};

function log_error() {
	local color_start='\033[31m'
	local color_end='\033[0m'
	_logging  ${color_start} ${color_end} 'ERROR' $1
};

function log_info() {
	local color_start='\033[32m'
	local color_end='\033[0m'
	_logging  ${color_start} ${color_end} 'INFO' $1
};

[ `whoami` == 'root']||{ log_error "请使用 root 用户运行该脚本";exit 10; }

# 创建系统用户
id ${RUN_USER} || useradd -M -s /sbin/nologin ${RUN_USER}
if [ $? -eq 0 ];then
	log_info "useradd -M -s /sbin/nologin ${RUN_USER}."
else
	log_error "useradd -M -s /sbin/nologin ${RUN_USER}执行失败，请检查用户是否已存在."
# 安装依赖
yum  -y install readline-devel pcre-devel openssl-devel perl gcc
if [ $? -eq 0 ];then
	log_info "yum  -y install readline-devel pcre-devel openssl-devel perl gcc执行成功."
else
	log_error "yum  -y install readline-devel pcre-devel openssl-devel perl gcc执行失败."
fi;
# 判断目录或文件是否存在,不存在则创建
[ -d ${DOWNLOAD_DIR} ]||mkdir -p ${DOWNLOAD_DIR}
[ $? -eq 0 ]||log_error "创建${DOWNLOAD_DIR}失败."

[ -d ${ABS_INSTALL_DIR} ] || mkdir -p ${ABS_INSTALL_DIR}
[ $? -eq 0 ]||log_error "创建${ABS_INSTALL_DIR}失败."
# 判断下载包是否存在，存在删除，解压目录是否存在，存在删除。
[ -f ${DOWNLOAD_DIR}/${TAR_PACKAGE_NAME} ] && rm -f ${DOWNLOAD_DIR}/${TAR_PACKAGE_NAME} 
[ $? -eq 0 ]||log_error "删除${DOWNLOAD_DIR}/${TAR_PACKAGE_NAME}失败."

[ -d ${DOWNLOAD_DIR}/${INSTALL_DIR} ] && mv   ${DOWNLOAD_DIR}/INSTALL_DIR ${DOWNLOAD_DIR}/INSTALL_DIR_`date +%F%m%d%H%M%S`
[ $? -eq 0 ]||log_error "删除${DOWNLOAD_DIR}/${INSTALL_DIR}失败."

# 下载安装包
cd ${DOWNLOAD_DIR}
curl -o ${TAR_PACKAGE_NAME} ${DOWNLOAD_URL}
[ $? -eq 0 ]||log_error "下载${TAR_PACKAGE_NAME}失败."

# 解压安装包
cd ${DOWNLOAD_DIR} && tar xf ${TAR_PACKAGE_NAME}
[ $? -eq 0 ]||log_error "解压${TAR_PACKAGE_NAME}失败."

# 编译安装包

cd ${DOWNLOAD_DIR}/${INSTALL_DIR} && ./configure --prefix=${ABS_INSTALL_DIR} --with-http_stub_status_module
[ $? -eq 0 ]||log_error "预编译${INSTALL_DIR}失败."

/usr/bin/gmake -j ${CPUS}
[ $? -eq 0 ]||log_error "编译${INSTALL_DIR}失败."

/usr/bin/gmake install
[ $? -eq 0 ]||log_error "安装${INSTALL_DIR}失败."

# 创建软链
ln -sv ${ABS_INSTALL_DIR} ${LINK}
[ $? -eq 0 ]||log_error "执行ln -sv ${ABS_INSTALL_DIR} ${LINK}失败."

echo 'export PATH=$PATH:/data/openresty/nginx/sbin' > /etc/profile.d/openresty.sh
chown -R ${RUN_USER}:${RUN_USER} ${ABS_INSTALL_DIR}

# 配置 systemctl 管理
cat >> /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server 
After=network.target remote-fs.target
WantedBy=multi-user.target

[Service]
Type=forking
User=root
PIDFile=${LINK}/nginx/logs/nginx.pid
ExecStartPre=${LINK}/nginx/sbin/nginx -t -c ${LINK}/nginx/conf/nginx.conf
ExecStart=${LINK}/nginx/sbin/nginx -c ${LINK}/nginx/conf/nginx.conf
ExecReload=${LINK}/nginx/sbin/nginx -s reload
ExecStop=${LINK}/nginx/sbin/nginx -s stop
ExecQuit=${LINK}/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF


systemctl daemon-reload
systemctl enable nginx
systemctl start nginx


######